import java.util.HashMap;
import java.util.Map;

public class testwork4 {
    public class Node{
        public int val;
        public Node next;
        public Node random;
        public Node(int val){
            this.val=val;
        }
    }
    //力扣：138. 随机链表的复制
    public Node copyRandomList(Node head) {
        Map<Node,Node> map=new HashMap<>();
        Node cur=head;
        //构建两个链表的基本数据val值相同地址不同
        while(cur!=null){
            Node ncur=new Node(cur.val);
            map.put(cur,ncur);
            cur=cur.next;
        }
        Node curN=head;
        //赋于其中next,random联系
        while(curN!=null){
            map.get(curN).next=map.get(curN.next);
            map.get(curN).random=map.get(curN.random);
            curN=curN.next;
        }
        return map.get(head);
    }
}
